package com.example.androidui;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MultiChoice extends DialogFragment {
    private List<String> selectItems = new ArrayList<>();
    private String[] genders = new String[]{"男","女"};
    private MultiChoice.NotifyDialogListener listener;

    public interface NotifyDialogListener {}

    public void setListener(MultiChoice.NotifyDialogListener listener) {
        this.listener = listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("多选对话框")
                .setMultiChoiceItems(genders, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            selectItems.add(genders[which]);
                        } else {
                            selectItems.remove(genders[which]);
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity().getApplicationContext(),"你选择了"+selectItems,Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity().getApplicationContext(),"你取消了！",Toast.LENGTH_SHORT).show();
                    }
                });
        return builder.create();
    }
}